local ziyougz = fk.CreateSkill {
    name = "ziyougz",
    anim_type = "offensive",
}

Fk:loadTranslationTable{
  ["ziyougz"] = "自由感召",
  ["$ziyougz1"] = "全体集结！",
  ["$ziyougz2"] = "团结制胜！",
  [":ziyougz"] = "你的回合结束时，若你的区域内没有<a href=':progoldsheild'>【振金盾牌】</a>，你可获得之，然后你可连续使用两张【杀】；此【杀】每造成一次伤害你摸一张牌。",
  ["#ziyougz-target"] = "自由感召：选择一名角色使用共计两张【杀】（第%arg张，共2张）！",
}

local U = require "packages.utility.utility"

ziyougz:addEffect(fk.TurnEnd, {
    can_trigger = function(self, event, target, player, data)
      if  player:hasSkill(ziyougz.name) and target == player then
        local has_hammer = false
        for _, id in ipairs(player:getCardIds{Player.Hand, Player.Equip, Player.Judge}) do
            if Fk:getCardById(id).name == "progoldsheild" then
                has_hammer = true
                break
            end
        end
        return not has_hammer
      end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room

        local allCardNames = {}
        for _, p in ipairs(room.players) do
            for _, id in ipairs(p:getCardIds("hej&")) do
                local card = Fk:getCardById(id)
                if card.name == "progoldsheild" then
                    table.insert(allCardNames, card)
                    break
                end
            end
        end
        if #allCardNames == 0 then 
            for _, id in ipairs(room.draw_pile) do
                local card = Fk:getCardById(id)
                if card.name == "progoldsheild" then
                    table.insert(allCardNames, card)
                    break
                end
            end
            if #allCardNames == 0 then 
                for _, id in ipairs(room.discard_pile) do
                    local card = Fk:getCardById(id)
                    if card.name == "progoldsheild" then
                        table.insert(allCardNames, card)
                        break
                    end
                end
                if #allCardNames == 0 then 
                    for _, id in ipairs(room.processing_area) do
                        local card = Fk:getCardById(id)
                        if card.name == "progoldsheild" then
                            table.insert(allCardNames, card)
                            break
                        end
                    end
                    if #allCardNames == 0 then 
                        for _, id in ipairs(room.void) do
                            local card = Fk:getCardById(id)
                            if card.name == "progoldsheild" then
                                table.insert(allCardNames, card)
                                break
                            end
                        end
                    end
                end
            end
        end
        if #allCardNames ~= 0 then 
            local dummy = Fk:cloneCard("dilu")
            dummy:addSubcards(allCardNames)
            room:obtainCard(player, dummy, true, fk.ReasonPrey, player, "ziyougz")
        end

        for i = 1, 2 do
            if player.dead or not room:askToUseVirtualCard(player, {
                name = "slash",
                skill_name = ziyougz.name,
                prompt = "#ziyougz-target:::" .. i,
                cancelable = true,
                extra_data = {
                    bypass_times = false,
                    extraUse = true,
                },
                }) then
              break
            end
        end
    end,
})

ziyougz:addEffect(fk.Damage, {
    is_delay_effect = true,
    mute = true,
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(ziyougz.name) and target == player and data.card then
            return data.card.skillName == ziyougz.name and data.card.name == "slash"
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        player:drawCards(1)
    end,
})

return ziyougz 